﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ImportDeclarationsBLL
    {
        private static readonly IAttachDA attachDA = DataAccess.CreateAttachDA();
        private static readonly IAttachDetailDA attachDetailDA = DataAccess.CreateAttachDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IImportDeclarationsDA importDeclarationsDA = DataAccess.CreateImportDeclarationsDA();
        private static readonly IImportDeclarationsDetailDA importDeclarationsDetailDA = DataAccess.CreateImportDeclarationsDetailDA();
        private static readonly IItemNoDA itemNoDA = DataAccess.CreateItemNoDA();
        private PubParasInfo pubParasInfo;
        private static readonly IReceiptDA receiptDA = DataAccess.CreateReceiptDA();
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();
        private static readonly IReceiptDetailLpnDA receiptDetailLpnDA = DataAccess.CreateReceiptDetailLpnDA();
        private static readonly IRotationRuleDA rotataionRuleDA = DataAccess.CreateRotationRuleDA();
        private static readonly IRotationRuleDetailDA rotataionRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();

        public ImportDeclarationsBLL()
        {
            this.pubParasInfo = null;
        }

        public ImportDeclarationsBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ImportDeclarationsBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDeclarationsInfo(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                string DID = IDS[0];
                ImportDeclarationsInfo importDeclarationsInfo = importDeclarationsDA.GetImportDeclarationsByID(dataBase, tran, DID, this.dataBaseID[0], this.pubParasInfo);
                importDeclarationsInfo.ImportDeclarationsDetailInfos = importDeclarationsDetailDA.GetImportDeclarationsDetailByID(dataBase, tran, DID, this.dataBaseID[0]);
                foreach (string id in IDS)
                {
                    ImportDeclarationsInfo info = importDeclarationsDA.GetImportDeclarationsByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    if (info.DeclarationsType.ToUpper() == "CR")
                    {
                        this.UpdateExternalIDByDID(info.DeclarationsID, "DELETE", info.OwnerID, info.ReceiptID, this.pubParasInfo);
                    }
                }
                DID = DID + "IMPORTD";
                List<AttachDetailInfo> attachDetailInfos = attachDetailDA.GetAttachDetailListByID(DID, this.dataBaseID[0]);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (importDeclarationsDA.DeleteImportDeclarations(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    auditDataBLL.InsertAuditData<ImportDeclarationsInfo>(dataBase, tran, importDeclarationsInfo, null, AuditAction.Delete, this.pubParasInfo.UserID, "IMPORT_DECLARATIONS", this.dataBaseID[0]);
                    if (importDeclarationsDetailDA.DeleteImportDeclarationsDetail(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                    {
                        for (int i = 0; i < importDeclarationsInfo.ImportDeclarationsDetailInfos.Count; i++)
                        {
                            if (receiptDA.GetReceiptByID(dataBase, tran, importDeclarationsInfo.ImportDeclarationsDetailInfos[i].ReceiptID, this.dataBaseID[0], this.pubParasInfo).IsPalletized == "Y")
                            {
                                receiptDetailLpnDA.UpdateReceiptDeclarationQtyByReceiptIDLineID(dataBase, tran, importDeclarationsInfo.ImportDeclarationsDetailInfos[i].ReceiptID, importDeclarationsInfo.ImportDeclarationsDetailInfos[i].ReceiptLineID.ToString(), importDeclarationsInfo.ImportDeclarationsDetailInfos[i].Qty, "DELETE", this.pubParasInfo.UserID, this.dataBaseID[0]);
                            }
                            else
                            {
                                receiptDetailDA.UpdateReceiptDeclarationQtyByReceiptIDLineID(dataBase, tran, importDeclarationsInfo.ImportDeclarationsDetailInfos[i].ReceiptID, importDeclarationsInfo.ImportDeclarationsDetailInfos[i].ReceiptLineID.ToString(), importDeclarationsInfo.ImportDeclarationsDetailInfos[i].Qty, "DELETE", this.pubParasInfo.UserID, this.dataBaseID[0]);
                            }
                            auditDataBLL.InsertAuditData<ImportDeclarationsDetailInfo>(dataBase, tran, importDeclarationsInfo.ImportDeclarationsDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "IMPORT_DECLARATIONS", this.dataBaseID[0]);
                        }
                    }
                    for (int n = 0; IDS.Count > n; n++)
                    {
                        temp = temp + IDS[n] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                if (attachDetailInfos.Count > 0)
                {
                    IDS[0] = DID;
                    attachDetailDA.DeleteAttachDetail(DID, this.dataBaseID[0]);
                    attachDA.DeleteAttach(IDS, this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllDeclarationsInfo()
        {
            return importDeclarationsDA.GetAllImportDeclarations(this.dataBaseID[0]);
        }

        public ImportDeclarationsInfo GetDeclarationsInfoByID(string id)
        {
            ImportDeclarationsInfo importDeclarationsInfo = importDeclarationsDA.GetImportDeclarationsByID(id, this.dataBaseID[0], this.pubParasInfo);
            if (importDeclarationsInfo != null)
            {
                importDeclarationsInfo.ImportDeclarationsDetailInfos = importDeclarationsDetailDA.GetImportDeclarationsDetailGroupBy(id, this.dataBaseID[0]);
                importDeclarationsInfo.AttachInfos = attachDA.GetAttachByID(id + "IMPORTD", this.dataBaseID[0]);
                if (importDeclarationsInfo.AttachInfos != null)
                {
                    importDeclarationsInfo.AttachInfos.AttachDetailInfos = attachDetailDA.GetAttachDetailListByID(id + "IMPORTD", this.dataBaseID[0]);
                }
            }
            return importDeclarationsInfo;
        }

        public DataSet GetDeclarationsInfoByQueryEntity(ImportDeclarationsQueryEntity importDeclarationsQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(importDeclarationsQueryEntity.DeclarationsID))
            {
                sqlWhere.Add(" IDS.DECLARATIONS_ID " + importDeclarationsQueryEntity.DeclarationsID);
            }
            if (!string.IsNullOrEmpty(importDeclarationsQueryEntity.OwnerID))
            {
                sqlWhere.Add(" IDS.OWNER_ID " + importDeclarationsQueryEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(importDeclarationsQueryEntity.DeclarationsType))
            {
                sqlWhere.Add(" IDS.DECLARATIONS_TYPE " + importDeclarationsQueryEntity.DeclarationsType);
            }
            if (!string.IsNullOrEmpty(importDeclarationsQueryEntity.ReceiptID))
            {
                sqlWhere.Add(" IDS.RECEIPT_ID " + importDeclarationsQueryEntity.ReceiptID);
            }
            if (!string.IsNullOrEmpty(importDeclarationsQueryEntity.DeclarationsDate))
            {
                sqlWhere.Add(importDeclarationsQueryEntity.DeclarationsDate.Replace("@DeclarationsDate@", "IDS.DECLARATIONS_DATE"));
            }
            if (!string.IsNullOrEmpty(importDeclarationsQueryEntity.Status))
            {
                sqlWhere.Add("IDS.STATUS " + importDeclarationsQueryEntity.Status);
            }
            return importDeclarationsDA.GetImportDeclarationsByQueryList(sqlWhere, importDeclarationsQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertDeclarationsInfo(ImportDeclarationsInfo importDeclarationsInfo)
        {
            if (!string.IsNullOrEmpty(importDeclarationsInfo.DeclarationsID) && importDeclarationsDA.CheckImportDeclarationsIDUnique(importDeclarationsInfo.DeclarationsID, this.dataBaseID[0]))
            {
                throw new Exception("此报关单号已存在!");
            }
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                if (importDeclarationsInfo.DeclarationsType.ToUpper() == "CR")
                {
                    this.UpdateExternalIDByDID(importDeclarationsInfo.DeclarationsID, "ADD", importDeclarationsInfo.OwnerID, importDeclarationsInfo.ReceiptID, this.pubParasInfo);
                }
                if (string.IsNullOrEmpty(importDeclarationsInfo.DeclarationsID))
                {
                    importDeclarationsInfo.DeclarationsID = commonMethod.GenNextNumStr(dataBase, tran, "IMPORT_DECLARATIONS", whLoginID);
                }
                importDeclarationsInfo.CreatedBy = this.pubParasInfo.UserID;
                importDeclarationsInfo.UpdatedBy = importDeclarationsInfo.CreatedBy;
                if (importDeclarationsDA.InsertImportDeclarations(dataBase, tran, importDeclarationsInfo, whLoginID) > 0)
                {
                    if ((importDeclarationsInfo.ImportDeclarationsDetailInfos != null) && (importDeclarationsInfo.ImportDeclarationsDetailInfos.Count > 0))
                    {
                        foreach (ImportDeclarationsDetailInfo DetailInfo in importDeclarationsInfo.ImportDeclarationsDetailInfos)
                        {
                            DetailInfo.DeclarationsID = importDeclarationsInfo.DeclarationsID;
                            DetailInfo.CreatedBy = this.pubParasInfo.UserID;
                            DetailInfo.UpdatedBy = DetailInfo.CreatedBy;
                            importDeclarationsDetailDA.InsertImportDeclarationsDetail(DetailInfo, whLoginID);
                        }
                    }
                    strDeclarationsID = importDeclarationsInfo.DeclarationsID;
                    new AuditDataBLL().InsertAuditData<ImportDeclarationsInfo>(dataBase, tran, null, importDeclarationsInfo, AuditAction.Insert, this.pubParasInfo.UserID, "IMPORT_DECLARATIONS", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        private string OrganizeRotationRule(RotationRuleInfo rotationRuleInfo)
        {
            string sqlOrderBy = string.Empty;
            if (rotationRuleInfo == null)
            {
                return sqlOrderBy;
            }
            if ((rotationRuleInfo.RotationRuleDetailInfo == null) || (rotationRuleInfo.RotationRuleDetailInfo.Count <= 0))
            {
                return sqlOrderBy;
            }
            sqlOrderBy = " ORDER BY ";
            for (int i = 0; i < rotationRuleInfo.RotationRuleDetailInfo.Count; i++)
            {
                string CS00002 = sqlOrderBy;
                sqlOrderBy = CS00002 + " IT." + rotationRuleInfo.RotationRuleDetailInfo[i].LotAttributes + " " + rotationRuleInfo.RotationRuleDetailInfo[i].Rotation + ",";
            }
            return sqlOrderBy.Substring(0, sqlOrderBy.Length - 1);
        }

        public ImportDeclarationsInfo ReceiptIDChanged(string recepitID, string type, string ownerID, PubParasInfo pubParasInfo)
        {
            List<ReceiptDetailInfo> receiptDetailinfo = new List<ReceiptDetailInfo>();
            string DeclarationsID = "";
            if (type != "CR")
            {
                ReceiptInfo receiptInfo = new ReceiptInfo();
                receiptInfo = receiptDA.GetReceiptByID(recepitID, this.dataBaseID[0], pubParasInfo);
                if ((receiptInfo == null) || !(receiptInfo.ReceiptID != ""))
                {
                    throw new Exception("入库单号:" + recepitID + "不存在!");
                }
                if (!(receiptInfo.DeclarationsType == "NCR"))
                {
                    throw new Exception("入库单:" + recepitID + "报关类型不是非集报,请重新输入或选择入库单!");
                }
                if (receiptInfo.IsDeclartion == "Y")
                {
                    throw new Exception("入库单:" + recepitID + "无法重复报关,请重新输入!");
                }
                if (!(!(receiptInfo.OwnerID != ownerID) || string.IsNullOrEmpty(ownerID)))
                {
                    throw new Exception("入库单:" + recepitID + "与货主不匹配,请重新输入!");
                }
                if ((receiptInfo.Status != "ReceivedComplete") && !(receiptInfo.Status == "Closed"))
                {
                    throw new Exception("入库单:" + recepitID + "状态不等于 收货完成或者 关闭 无法进行报关操作!");
                }
                DeclarationsID = receiptInfo.ExternalReceiptID;
                if (receiptInfo.IsPalletized == "Y")
                {
                    receiptDetailinfo = receiptDetailLpnDA.GetReceiptDetailLpnListByID(recepitID, this.dataBaseID[0], pubParasInfo);
                }
                else
                {
                    receiptDetailinfo = receiptDetailDA.GetReceiptDetailListByID(recepitID, this.dataBaseID[0], pubParasInfo);
                }
            }
            else
            {
                List<ReceiptInfo> receiptInfos = receiptDA.GetReceiptByCRID(recepitID, ownerID, this.dataBaseID[0], pubParasInfo);
                if ((receiptInfos == null) || (receiptInfos.Count <= 0))
                {
                    throw new Exception("核放单号:" + recepitID + "未找到匹配的数据源!");
                }
                foreach (ReceiptInfo info in receiptInfos)
                {
                    if (info.IsDeclartion != "Y")
                    {
                        if (info.IsPalletized == "Y")
                        {
                            info.ReceiptDetailInfos = receiptDetailLpnDA.GetReceiptDetailLpnListByID(info.ReceiptID, this.dataBaseID[0], pubParasInfo);
                        }
                        else
                        {
                            info.ReceiptDetailInfos = receiptDetailDA.GetReceiptDetailListByID(info.ReceiptID, this.dataBaseID[0], pubParasInfo);
                        }
                        if ((info.ReceiptDetailInfos != null) && (info.ReceiptDetailInfos.Count > 0))
                        {
                            foreach (ReceiptDetailInfo detailinfo in info.ReceiptDetailInfos)
                            {
                                receiptDetailinfo.Add(detailinfo);
                            }
                        }
                    }
                }
            }
            List<ImportDeclarationsDetailInfo> importDeclarationsDetailInfos = new List<ImportDeclarationsDetailInfo>();
            if ((receiptDetailinfo != null) && (receiptDetailinfo.Count > 0))
            {
                for (int i = 0; receiptDetailinfo.Count > i; i++)
                {
                    if ((receiptDetailinfo[i].ReceivedQty - receiptDetailinfo[i].DeclarationQty) > 0M)
                    {
                        ImportDeclarationsDetailInfo importDeclarationsDetailInfo = new ImportDeclarationsDetailInfo {
                            LineID = (i + 1) * 10,
                            ReceiptID = receiptDetailinfo[i].ReceiptID,
                            ReceiptLineID = receiptDetailinfo[i].ReceiptLineID,
                            SkuID = receiptDetailinfo[i].SkuID,
                            SkuDescr = receiptDetailinfo[i].SkuDescr,
                            AltSkuID = receiptDetailinfo[i].AltSkuID,
                            ItemNoID = receiptDetailinfo[i].ReceiptLot.LotAttr03
                        };
                        if (!string.IsNullOrEmpty(receiptDetailinfo[i].LotAttr03))
                        {
                            importDeclarationsDetailInfo.ItemNoDescr = itemNoDA.GetItemNoDescrByID(receiptDetailinfo[i].LotAttr03, this.dataBaseID[0]);
                        }
                        importDeclarationsDetailInfo.OwnerID = receiptDetailinfo[i].OwnerID;
                        importDeclarationsDetailInfo.CaseID = receiptDetailinfo[i].CaseID;
                        importDeclarationsDetailInfo.Status = "New";
                        importDeclarationsDetailInfo.UomID = receiptDetailinfo[i].UomID;
                        importDeclarationsDetailInfo.PackID = receiptDetailinfo[i].PackID;
                        importDeclarationsDetailInfo.Cube = Convert.ToDecimal((float) (receiptDetailinfo[i].Cube * Convert.ToSingle(receiptDetailinfo[i].ReceivedQty)));
                        importDeclarationsDetailInfo.GrossWgt = Convert.ToDecimal((float) (receiptDetailinfo[i].GrossWgt * Convert.ToSingle(receiptDetailinfo[i].ReceivedQty)));
                        importDeclarationsDetailInfo.NetWgt = Convert.ToDecimal((float) (receiptDetailinfo[i].NetWgt * Convert.ToSingle(receiptDetailinfo[i].ReceivedQty)));
                        importDeclarationsDetailInfo.OtherUnit1 = Convert.ToDecimal(receiptDetailinfo[i].OtherUnit1);
                        importDeclarationsDetailInfo.OtherUnit2 = Convert.ToDecimal(receiptDetailinfo[i].OtherUnit2);
                        importDeclarationsDetailInfo.UnitPrice = Convert.ToDecimal(receiptDetailinfo[i].UnitPrice);
                        importDeclarationsDetailInfo.ExtendedPrice = 0M;
                        importDeclarationsDetailInfo.Udf1 = receiptDetailinfo[i].Udf1;
                        importDeclarationsDetailInfo.Udf2 = receiptDetailinfo[i].Udf2;
                        importDeclarationsDetailInfo.Udf3 = receiptDetailinfo[i].Udf3;
                        importDeclarationsDetailInfo.Udf4 = receiptDetailinfo[i].Udf4;
                        importDeclarationsDetailInfo.Udf5 = receiptDetailinfo[i].Udf5;
                        importDeclarationsDetailInfo.Udf6 = receiptDetailinfo[i].Udf6;
                        importDeclarationsDetailInfo.Udf7 = receiptDetailinfo[i].Udf7;
                        importDeclarationsDetailInfo.Udf8 = receiptDetailinfo[i].Udf8;
                        importDeclarationsDetailInfo.Udf9 = receiptDetailinfo[i].Udf9;
                        importDeclarationsDetailInfo.Udf10 = receiptDetailinfo[i].Udf10;
                        importDeclarationsDetailInfo.Remark = receiptDetailinfo[i].Remark;
                        importDeclarationsDetailInfo.CreatedBy = receiptDetailinfo[i].CreatedBy;
                        importDeclarationsDetailInfo.CreatedDate = receiptDetailinfo[i].CreatedDate;
                        importDeclarationsDetailInfo.UpdatedBy = receiptDetailinfo[i].UpdatedBy;
                        importDeclarationsDetailInfo.UpdatedDate = receiptDetailinfo[i].UpdatedDate;
                        importDeclarationsDetailInfo.Ts = receiptDetailinfo[i].Ts;
                        importDeclarationsDetailInfo.Qty = 0M;
                        importDeclarationsDetailInfo.ReceivedQty = receiptDetailinfo[i].ReceivedQty;
                        importDeclarationsDetailInfo.DifferencesQty = receiptDetailinfo[i].ReceivedQty - receiptDetailinfo[i].DeclarationQty;
                        importDeclarationsDetailInfos.Add(importDeclarationsDetailInfo);
                    }
                }
            }
            return new ImportDeclarationsInfo { DeclarationsID = DeclarationsID, DeclarationsType = type, ImportDeclarationsDetailInfos = importDeclarationsDetailInfos };
        }

        public string UpdateDeclarationsInfo(ImportDeclarationsInfo importDeclarationsInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                importDeclarationsInfo.UpdatedBy = this.pubParasInfo.UserID;
                ImportDeclarationsInfo oldImportDeclarationsInfo = importDeclarationsDA.GetImportDeclarationsByID(dataBase, tran, importDeclarationsInfo.DeclarationsID, whLoginID, this.pubParasInfo);
                if (importDeclarationsDA.UpdateImportDeclarations(dataBase, tran, importDeclarationsInfo, whLoginID) > 0)
                {
                    Dictionary<string, SkuInfo> skuObjects = new Dictionary<string, SkuInfo>();
                    Dictionary<string, RotationRuleInfo> rotationRuleObjects = new Dictionary<string, RotationRuleInfo>();
                    Dictionary<string, PackInfo> packInfoObjects = new Dictionary<string, PackInfo>();
                    if ((importDeclarationsInfo.ImportDeclarationsDetailInfos != null) && (importDeclarationsInfo.ImportDeclarationsDetailInfos.Count > 0))
                    {
                        foreach (ImportDeclarationsDetailInfo DetailInfo in importDeclarationsInfo.ImportDeclarationsDetailInfos)
                        {
                            SkuInfo skuinfo = null;
                            if (!skuObjects.ContainsKey(DetailInfo.OwnerID + DetailInfo.SkuID))
                            {
                                skuinfo = skuDA.GetSkuByID(dataBase, tran, DetailInfo.OwnerID, DetailInfo.SkuID, whLoginID);
                                skuObjects.Add(DetailInfo.OwnerID + DetailInfo.SkuID, skuinfo);
                            }
                            else
                            {
                                skuObjects.TryGetValue(DetailInfo.OwnerID + DetailInfo.SkuID, out skuinfo);
                            }
                            RotationRuleInfo rotationRuleInfo = null;
                            if (!rotationRuleObjects.ContainsKey(skuinfo.RotationRuleID))
                            {
                                rotationRuleInfo = rotataionRuleDA.GetRotationRuleByID(dataBase, tran, skuinfo.RotationRuleID, this.dataBaseID[0]);
                                if (rotationRuleInfo != null)
                                {
                                    rotationRuleInfo.RotationRuleDetailInfo = rotataionRuleDetailDA.GetRotationRuleDetailListByID(dataBase, tran, rotationRuleInfo.RotationRuleID, this.dataBaseID[0]);
                                    rotationRuleObjects.Add(rotationRuleInfo.RotationRuleID, rotationRuleInfo);
                                }
                            }
                            else
                            {
                                rotationRuleObjects.TryGetValue(skuinfo.RotationRuleID, out rotationRuleInfo);
                            }
                            string orderBy = this.OrganizeRotationRule(rotationRuleInfo);
                            List<ImportDeclarationsDetailInfo> importDeclarationsDetailList = importDeclarationsDetailDA.GetImportDeclarationsDetailInfoByRotationRule(dataBase, tran, DetailInfo, orderBy, whLoginID);
                            if ((importDeclarationsDetailList != null) && (importDeclarationsDetailList.Count > 0))
                            {
                                foreach (ImportDeclarationsDetailInfo info in importDeclarationsDetailList)
                                {
                                    List<ImportDeclarationsDetailInfo> importDeclarationsDetail = importDeclarationsDetailDA.GetImportDeclarationsDetailReceiptIDLineID(dataBase, tran, info.ReceiptID, info.ReceiptLineID.ToString(), whLoginID);
                                    decimal qty = 0M;
                                    decimal newqty = 0M;
                                    if ((importDeclarationsDetail != null) && (importDeclarationsDetail.Count > 0))
                                    {
                                        foreach (ImportDeclarationsDetailInfo qtyinfo in importDeclarationsDetail)
                                        {
                                            qty += qtyinfo.Qty;
                                        }
                                    }
                                    qty = info.ReceivedQty - qty;
                                    if ((qty + info.Qty) >= DetailInfo.Qty)
                                    {
                                        newqty = DetailInfo.Qty;
                                        qty = DetailInfo.Qty;
                                        DetailInfo.Qty = 0M;
                                    }
                                    else
                                    {
                                        newqty = qty + info.Qty;
                                        DetailInfo.Qty -= newqty;
                                    }
                                    decimal extendedPrice = newqty * info.UnitPrice;
                                    importDeclarationsDetailDA.UpdateImportDeclarationsDetailQtyByDeclarationsID(dataBase, tran, info.DeclarationsID, info.LineID.ToString(), newqty, extendedPrice, this.pubParasInfo.UserID, whLoginID);
                                    if (receiptDA.GetReceiptByID(dataBase, tran, info.ReceiptID, whLoginID, this.pubParasInfo).IsPalletized == "Y")
                                    {
                                        receiptDetailLpnDA.UpdateReceiptDeclarationQtyByReceiptIDLineID(dataBase, tran, info.ReceiptID, info.ReceiptLineID.ToString(), newqty - info.Qty, "", this.pubParasInfo.UserID, whLoginID);
                                    }
                                    else
                                    {
                                        receiptDetailDA.UpdateReceiptDeclarationQtyByReceiptIDLineID(dataBase, tran, info.ReceiptID, info.ReceiptLineID.ToString(), newqty - info.Qty, "", this.pubParasInfo.UserID, whLoginID);
                                    }
                                    if (DetailInfo.Qty == 0M)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    strDeclarationsID = importDeclarationsInfo.DeclarationsID;
                    new AuditDataBLL().InsertAuditData<ImportDeclarationsInfo>(dataBase, tran, oldImportDeclarationsInfo, importDeclarationsInfo, AuditAction.Update, this.pubParasInfo.UserID, "IMPORT_DECLARATIONS", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        public string UpdateDeclarationsInfoData(ImportDeclarationsInfo importDeclarationsInfo, PubParasInfo pubParasInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                importDeclarationsInfo.UpdatedBy = pubParasInfo.UserID;
                if ((importDeclarationsDA.UpdateImportDeclarations(importDeclarationsInfo, whLoginID) > 0) && ((importDeclarationsInfo.ImportDeclarationsDetailInfos != null) && (importDeclarationsInfo.ImportDeclarationsDetailInfos.Count > 0)))
                {
                    foreach (ImportDeclarationsDetailInfo DetailInfo in importDeclarationsInfo.ImportDeclarationsDetailInfos)
                    {
                        List<ImportDeclarationsDetailInfo> importDeclarationsDetailInfoList = importDeclarationsDetailDA.GetImportDeclarationsDetail(dataBase, tran, DetailInfo.DeclarationsID, DetailInfo.SkuID, whLoginID);
                        decimal i = importDeclarationsDetailInfoList.Count;
                        if (i != 0M)
                        {
                            foreach (ImportDeclarationsDetailInfo info in importDeclarationsDetailInfoList)
                            {
                                info.GrossWgt = DetailInfo.GrossWgt / i;
                                info.NetWgt = DetailInfo.NetWgt / i;
                                info.Cube = DetailInfo.Cube / i;
                                decimal CS00004 = decimal.Parse(DetailInfo.Udf1) / i;
                                info.Udf1 = CS00004.ToString();
                                info.Udf2 = (decimal.Parse(DetailInfo.Udf2) / i).ToString();
                                info.UnitPrice = DetailInfo.UnitPrice;
                                info.ExtendedPrice = info.Qty * DetailInfo.UnitPrice;
                                importDeclarationsDetailDA.UpdateImportDeclarationsDetailByDeclarationsID(dataBase, tran, info, pubParasInfo.UserID, whLoginID);
                            }
                        }
                    }
                    strDeclarationsID = importDeclarationsInfo.DeclarationsID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        public string UpdateDeclarationsStatus(string declarationsID, string status, string verificationFormID, PubParasInfo pubParasInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strDeclarationsID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(verificationFormID))
                {
                    importDeclarationsDA.UpdateImportDeclarationsStatusByID(dataBase, tran, declarationsID, verificationFormID, status, pubParasInfo.UserID, whLoginID);
                }
                else
                {
                    importDeclarationsDA.UpdateImportDeclarationsStatusByID(dataBase, tran, declarationsID, status, pubParasInfo.UserID, whLoginID);
                }
                importDeclarationsDetailDA.UpdateImportDeclarationsDetailStatusByID(dataBase, tran, declarationsID, status, pubParasInfo.UserID, whLoginID);
                strDeclarationsID = declarationsID;
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strDeclarationsID;
        }

        private void UpdateExternalIDByDID(string declarationsID, string flag, string ownerID, string receiptID, PubParasInfo pubParasInfo)
        {
            if (flag.ToUpper() == "ADD")
            {
                receiptDA.UpdateExternalReceiptIDByDIDcr(declarationsID, receiptID, this.dataBaseID[0], pubParasInfo);
            }
            else
            {
                List<ReceiptInfo> receiptInfoList = receiptDA.GetReceiptByCRID(receiptID, ownerID, this.dataBaseID[0], pubParasInfo);
                foreach (ReceiptInfo info in receiptInfoList)
                {
                    if (info.ExternalReceiptID.Contains(declarationsID))
                    {
                        if (info.ExternalReceiptID == declarationsID)
                        {
                            info.ExternalReceiptID = info.ExternalReceiptID.Replace(declarationsID, "");
                        }
                        else
                        {
                            info.ExternalReceiptID = info.ExternalReceiptID.Replace(declarationsID + ",", "");
                            if (info.ExternalReceiptID.Contains(declarationsID))
                            {
                                info.ExternalReceiptID = info.ExternalReceiptID.Replace("," + declarationsID, "");
                            }
                        }
                        receiptDA.UpdateReceipt(info, this.dataBaseID[0]);
                    }
                }
            }
        }
    }
}

